package com.googlecode.maratische.google.gui;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

import org.apache.log4j.Logger;

import be.lechtitseb.google.reader.api.model.feed.ItemDescriptor;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.DaoAction;
import com.googlecode.maratische.google.model.Item;
import com.googlecode.maratische.google.model.Label;
import com.googlecode.maratische.google.utils.StringUtils;

public class ItemListModel implements ListModel {
	static Logger logger = Logger.getLogger(ItemListModel.class.getName());

	private List<Item> items = null;
	private List<String> itemStrings = null;
	private boolean expandItem = false;
	
	public void doExpandItems(boolean expand) {
		this.expandItem = expand;
	}
	
	public List<Item> getItems() {
		if (this.items == null) {
			this.items = new ArrayList<Item>();
		}
		return this.items;
	}
	
	public List<String> getItemStrings() {
		if (this.itemStrings == null) {
			this.itemStrings = new ArrayList<String>();
		}
		return this.itemStrings;
	}

	
	/**
	 * обновляем конкретный элемент.
	 * @param index
	 */
	public void updateItemStringForItem(int index) {
		this.itemStrings.remove(index);
		this.itemStrings.add(index, itemToString(this.items.get(index)));
	}
	
	public void clearItems() {
		this.getItems().clear();
		this.getItemStrings().clear();
	}
	
	public void add(Item item) {
		this.items.add(item);
		this.getItemStrings().add(itemToString(item));
	}
	
	public void addAll(List<Item> items) {
		if (items != null && items.size()>0) {
			for (Item item : items) {
				this.add(item);
			}
		}
	}

	public void add(ItemDescriptor itemDescriptor) {
		this.add(new Item(itemDescriptor));
	}
	
	public Item getItem(int index) {
		if (index>-1 && index<this.getItems().size()) {
			return this.getItems().get(index);
		}
		return null;
	}
	
	public void addListDataListener(ListDataListener l) {
	}

	private SimpleDateFormat dateFormat =new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
	
	private String itemToString(Item item) {
		StringBuffer stringBuffer = new StringBuffer("<html><body>");
		try {
			if (item.getLabelId() != null) {
				Label label = DaoAction.getDaoAction().getLabelDao().get(item.getLabelId());
				if (label.getFileSaved() != null && label.getFileSaved().length()>0) {
					stringBuffer.append("[d]");
				} else {
					stringBuffer.append("[*]");
				}
				stringBuffer.append(label.getType());
				stringBuffer.append(":");
			}
		} catch (SQLException e) {
			logger.error(e.getStackTrace());
		} catch (BaseException e) {
			logger.error(e.getStackTrace());
		}
		//название элемента
		stringBuffer.append(StringUtils.removeXml(item.getTitle()));
		stringBuffer.append(" ");
		//дата
		if (item.getUpdatedDate() != null) {
			stringBuffer.append("<span style=\"font-style: italic; color: gray\">");
			stringBuffer.append(dateFormat.format(item.getUpdatedDate()));
			stringBuffer.append("</span>");
		}
		if (expandItem) {
			String descr = item.getDescription(); 
			if (descr != null) {
				descr = descr.length() < 300 ? descr : descr.substring(0,297)+"...";
				stringBuffer.append("<br/>");
				stringBuffer.append("<span style=\"font-style: italic; color: gray\">");
				stringBuffer.append(StringUtils.removeXml( descr ));
				stringBuffer.append("</span>");
			}
		}
		stringBuffer.append("</body></html>");
		return stringBuffer.toString();
	}
	
	public Object getElementAt(int index) {
		return getItemStrings().get(index);
	}

	public int getSize() {
		return getItems().size();
	}

	public void removeListDataListener(ListDataListener l) {
	}

}
